
function rend(txt, data, r, c){
	if(c===6){
		if(data.hasRef==false){
			return  "<span class='font_bold'>"+$.i18n('common.no')+"</span>"
		}else{
			return  "<span class='font_bold'>"+$.i18n('common.yes')+"</span>"
		}
	}
	return txt;
}


//新建消息规则
function addRule(){
    
	 var adddialog = $.dialog({
         targetWindow:getCtpTop(),
         id: 'addRuledialog',
         url: _ctxPath +"/messageRule.do?method=messageRuleDetail&formAppId="+formAppId,
         width: 540,
         height: 440,
         title: $.i18n('message.rule.add.title'),//系统提示
         buttons: [{
             text: $.i18n('collaboration.button.ok.label'), //确定
             handler: function () {
                 adddialog.getReturnValue({"submitCallback" : function(){
                     
                     setTimeout(function(){
                         adddialog.close();
                      // 页面刷新
                         window.location.reload();
                     }, 50)
                 }});
             }
         }, {
             text: $.i18n('collaboration.pushMessageToMembers.cancel'), //取消
             handler: function () {
            	 adddialog.close();
             }
         }]
     });
}
//修改消息规则
function updateRule(openForm) {
    
    var rows = grid.grid.getSelectRows();
    if (rows.length != 1) {
        $.alert($.i18n("message.rule.selectOneUpdate.js"));
        return true;
    }
    var canEdit = true;
    var isPresetData = false;
    var isAdminCreate = false;

    if (rows[0].isSystem) {

        if (rows[0].creater == '1') {
            isPresetData = true;
            canEdit = false;
        } else if (!$.ctx.CurrentUser.isAdmin) {
            isAdminCreate = true;
            canEdit = false;
        }
    }

    if (openForm != 'click') {
        if (isPresetData) {
            $.alert($.i18n("message.rule.systemDefaultNotUpdate.js"))
            return true;
        }

        if (isAdminCreate) {
            $.alert($.i18n("message.rule.systemCreateNotUpdate.js"))
            return true;
        }
    }

    var _btns = [];
    var title = "";

    if (canEdit) {
        title = $.i18n('message.rule.update.title');
        _btns.push({
            text : $.i18n('collaboration.button.ok.label'), // 确定
            handler : function() {
                
                adddialog.getReturnValue({"submitCallback" : function(){
                    
                    setTimeout(function(){
                        adddialog.close();
                     // 页面刷新
                        window.location.reload();
                    }, 50)
                }});
            }
        });
    } else {
        title = $.i18n('message.rule.show.title.js')
    }

    _btns.push({
        text : $.i18n('collaboration.pushMessageToMembers.cancel'), // 取消
        handler : function() {
            adddialog.close();
        }
    });

    var adddialog = $.dialog({
        targetWindow : getCtpTop(),
        id : 'addRuledialog',
        url : _ctxPath + "/messageRule.do?method=messageRuleDetail&ruleId=" + rows[0].id + "&formAppId=" + formAppId + "&canEdit=" + canEdit,
        width : 540,
        height : 440,
        title : title,//系统提示
        buttons : _btns
    });
}


//删除消息规则
function deleteRule(){
	
	  var rows = grid.grid.getSelectRows();
	  if(rows.length != 1) {
	      // 请选择要删除的协同。
	      $.alert($.i18n("message.rule.selectOneDelete.js"));
	      return true;
	   }
	  
	  if(rows[0].isSystem && rows[0].creater=='1'){
		  $.alert($.i18n("message.rule.systemDefaultNotDelete.js"))
		  return true;
	  }
	  
	  if(rows[0].isSystem && !$.ctx.CurrentUser.isAdmin){
		  $.alert($.i18n("message.rule.systemCreateNotDelete.js"))
		  return true;
	  }
	  
	  var ruleId = rows[0].id;
	  
	  var ruleManager = new messageRuleManager();
	  var hasRef = ruleManager.checkMessageRuleHasRef(ruleId);
	  if(hasRef==true){
		  $.alert($.i18n("message.rule.hasRefNotDelete.js"))
		  return true;
	  }
	  // end
	  var confirm = $.confirm({
	          // 该操作不能恢复，是否进行删除操作
	      'msg': $.i18n('collaboration.confirmDelete'),
	      ok_fn: function () {
	          
	            var obj = new Object();
	            obj.id = ruleId;
	            callBackendMethod("messageRuleManager","deleteRuleById",ruleId,{
	                success : function(flag){
	                	if(flag=="true"){
	                		$("#listRule").ajaxgridLoad();
	                	}else{
	                		$.alert($.i18n("message.rule.notDelete.js"));
	                	}
	                }, 
	                error : function(request, settings, e){
	                    $.alert(e);
	                }
	            
	            });
	      },
	      "cancel_fn":function(){
	      }
	  });

}

function copyRule() {
    var rows = grid.grid.getSelectRows();
    if (rows.length != 1) {
        $.alert($.i18n("message.rule.selectOneCopy.js"));
        return true;
    }

    var adddialog = $.dialog({
        targetWindow : getCtpTop(),
        id : 'addRuledialog',
        url : _ctxPath + "/messageRule.do?method=messageRuleDetail&ruleId=" + rows[0].id + "&formAppId=" + formAppId + "&openForm=copy",
        width : 540,
        height : 500,
        title : $.i18n('message.rule.update.label'),// 系统提示
        buttons : [ {
            text : $.i18n('collaboration.button.ok.label'), // 确定
            handler : function() {
                
                adddialog.getReturnValue({"submitCallback" : function(){
                    
                    setTimeout(function(){
                        adddialog.close();
                     // 页面刷新
                        window.location.reload();
                    }, 50)
                }});
            }
        }, {
            text : $.i18n('collaboration.pushMessageToMembers.cancel'), // 取消
            handler : function() {
                adddialog.close();
            }
        } ]
    });
}

function dbclickRow(){
	updateRule('click');
}

/**
 * kk 邮件增强 start
 * 
 * @param openForm
 * @returns {Boolean}
 */
function strongRule(openForm) {

	var rows = grid.grid.getSelectRows();
	if (rows.length != 1) {
		$.alert($.i18n("message.rule.selectOneUpdate.js"));
		return true;
	}
	var canEdit = true;
	var emailRowsId = "";
	if (rows[0].isSystem) {
		// 系统预制消息key
		emailRowsId=rows[0].messageTypeShow == "送达提醒"?"col.send":"node.affair.overTerm";
	}else{
		emailRowsId = rows[0].id;
	}
	// 跳转页面
	if (canEdit) {
		// 配置id
		$.ajax({
			url : _ctxPath + '/kkES.do?method=showEmailStrong',
			type : 'POST',
			data : {
				emailRowsId : emailRowsId,
				formAppId : formAppId
			},
			async : false,
			dataType : 'json',
			success : function(result) {
				window.open(_ctxPath+result.url,'_blank','top=10,resizable=yes')
			},
			error : function() {
				console.log("邮件增强请求错误！");
			}
		});
	}

}
// kk 邮件增强 end

var grid;
$(document).ready(function(){

	new MxtLayout({
        'id': 'layout',
        'northArea': {
            'id': 'north',
            'height': 40,
            'sprit': false,
            'border': false
        },
        'centerArea': {
            'id': 'center',
            'border': false,
            'minHeight': 20
        }
    });
	
    var toolbarArray = new Array();
    //新建
    toolbarArray.push({id: "addRule",name: $.i18n('message.rule.add.label'),className: "ico16",click: addRule});
    
    //修改
    toolbarArray.push({id: "updateRule",name: $.i18n('message.rule.update.label'),className: "ico16 editor_16",click:updateRule});
    //删除
    toolbarArray.push({id: "deleteRule",name: $.i18n('message.rule.del.label'),className: "ico16 del_16",click:deleteRule});
    //复制流程
    toolbarArray.push({id: "copyRule",name:$.i18n('message.rule.copy.label'),className: "ico16 copy_from_16",click:copyRule});
	// kk 邮件增强按钮 start
	toolbarArray.push({
		id : "strongRule",
		name : "邮件增强",
		className : "ico16 editor_16",
		click : strongRule
	});
	// kk end
    //工具栏
    $("#toolbars").toolbar({
        toolbar: toolbarArray
    });
	
	grid = $('#listRule').ajaxgrid({
        colModel: [{
            display: 'id',
            name: 'id',
            width: 'smallest',
            type: 'checkbox',
            isToggleHideShow:false,
            align:'center'
        }, {
            display: $.i18n('message.rule.type.label'),
            name: 'messageTypeShow',
            sortable : true,
            width: 'small'
        },{
            display: $.i18n('message.rule.name.label'),
            name: 'messageSubject',
            sortable : true,
            width: 'medium'
        },{
            display: $.i18n('message.rule.receivers.label'),
            name: 'receiversShow',
            sortable : true,
            width: 'medium'
        }, {
            display: $.i18n('message.rule.reminder.label'),
            name: 'messageModeShow',
            sortable : true,
            width: 'medium'
        }, {
            display: $.i18n('message.rule.creater.label'),
            name: 'createrShow',
            sortable : true,
            width: 'small'
        }, {
            display: $.i18n('message.rule.hasRef.js'),
            name: 'extInfo',
            sortable : true,
            width: 'small'
        }],
        click: dbclickRow,
        render : rend,
        height: 200,
        showTableToggleBtn: true,
        gridType:'autoGrid',
        parentId: $('.layout_center').eq(0).attr('id'),
        vChange: true,
        vChangeParam: {
            overflow: "hidden",
            autoResize:false //表格下方是否自动显示
        },
        isHaveIframe:true,
        slideToggleBtn:true,
        managerName : "messageRuleManager",
        managerMethod : "getRuleList"
    });
	var o = new Object();
	o.formAppId = formAppId;
	$('#listRule').ajaxgridLoad(o);
});